package org.example.app.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 品类挖掘结果实体类 - 符合实时数仓设计规范
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class CategoryMiningResult extends BaseDwsBean {
    
    // 业务主键
    private String categoryId;
    private String categoryName;
    
    // 层级维度
    private String parentCategoryId;
    private String parentCategoryName;
    private Integer categoryLevel;
    
    // 销售度量字段
    private Double totalSalesAmount;
    private Long totalSalesCount;
    private Long productCount;
    private Long brandCount;
    private Long shopCount;
    private Double avgPrice;
    
    // 市场分析字段
    private Double marketShare;
    private Double currentSales;
    private Double previousSales;
    private Double growthRate;
    private String growthTrend;         // rising/stable/declining
    
    // 供需分析字段
    private Long demandCount;           // 需求量（搜索量等）
    private Long supplyCount;           // 供给量（商品数量）
    private Double supplyDemandRatio;   // 供需比
    private String supplyDemandLevel;   // oversupply/balanced/shortage
    
    // 机会分析字段
    private String categoryType;        // 品类类型：star/cash_cow/question_mark/dog
    private Double opportunityScore;    // 机会分数
    private String recommendAction;     // 推荐动作：invest/maintain/harvest/divest
    
    // 竞争分析字段
    private Integer competitorCount;
    private Double competitionIntensity;
    private Double entryBarrier;        // 进入门槛
    
    /**
     * 计算增长率
     */
    public void calculateGrowthRate() {
        if (previousSales != null && previousSales > 0 && currentSales != null) {
            this.growthRate = (currentSales - previousSales) / previousSales * 100;
            
            // 设置增长趋势
            if (growthRate > 10) {
                this.growthTrend = "rising";
            } else if (growthRate > -5) {
                this.growthTrend = "stable";
            } else {
                this.growthTrend = "declining";
            }
        } else {
            this.growthRate = 0.0;
            this.growthTrend = "unknown";
        }
    }
    
    /**
     * 计算供需比
     */
    public void calculateSupplyDemandRatio() {
        if (demandCount != null && demandCount > 0 && supplyCount != null && supplyCount > 0) {
            this.supplyDemandRatio = supplyCount.doubleValue() / demandCount.doubleValue();
            
            // 设置供需水平
            if (supplyDemandRatio > 2.0) {
                this.supplyDemandLevel = "oversupply";
            } else if (supplyDemandRatio > 0.5) {
                this.supplyDemandLevel = "balanced";
            } else {
                this.supplyDemandLevel = "shortage";
            }
        } else {
            this.supplyDemandRatio = 0.0;
            this.supplyDemandLevel = "unknown";
        }
    }
    
    /**
     * 分析品类类型（BCG矩阵）
     */
    public void analyzeCategoryType() {
        if (marketShare != null && growthRate != null) {
            boolean highGrowth = growthRate > 10;
            boolean highShare = marketShare > 10;
            
            if (highGrowth && highShare) {
                this.categoryType = "star";
                this.recommendAction = "invest";
            } else if (!highGrowth && highShare) {
                this.categoryType = "cash_cow";
                this.recommendAction = "maintain";
            } else if (highGrowth && !highShare) {
                this.categoryType = "question_mark";
                this.recommendAction = "invest";
            } else {
                this.categoryType = "dog";
                this.recommendAction = "divest";
            }
        } else {
            this.categoryType = "unknown";
            this.recommendAction = "analyze";
        }
    }
    
    /**
     * 计算机会分数
     */
    public void calculateOpportunityScore() {
        double score = 0.0;
        
        // 增长率权重 40%
        if (growthRate != null) {
            score += Math.min(growthRate / 50.0, 1.0) * 40;
        }
        
        // 市场份额权重 30%
        if (marketShare != null) {
            score += Math.min(marketShare / 30.0, 1.0) * 30;
        }
        
        // 供需状况权重 20%
        if (supplyDemandRatio != null) {
            if (supplyDemandRatio < 1.0) { // 供不应求
                score += 20;
            } else if (supplyDemandRatio < 2.0) {
                score += 10;
            }
        }
        
        // 竞争强度权重 10%（竞争越小分数越高）
        if (competitionIntensity != null) {
            score += (1.0 - competitionIntensity) * 10;
        }
        
        this.opportunityScore = Math.min(score, 100.0);
    }
}